import time
from selenium.webdriver.common.by import By


from Hogwarts.Homeworks.hw0627.page_objects.base_page import BasePage

"""
PAGE：通讯录页面
"""


class ContactPage(BasePage):
    # OBJECTS
    _ADD_MEMBER = (By.CSS_SELECTOR, ".ww_operationBar .js_add_member")
    _ADD_DEPT = (By.XPATH, '//*[@id="1688850007082185_anchor"]/span')
    _MENU_ADD_SUB_DEPT = (By.CSS_SELECTOR, "body > ul > li:nth-child(1) > a")
    _PERSON_NAMES = (By.CSS_SELECTOR, ".member_colRight_memberTable_td:nth-child(2)")
    _DEPT_NAMES = (By.XPATH, '//a[@class="jstree-anchor"]')

    def click_add_member(self):
        """
        点击"添加成员"
        :return:
        """
        from Hogwarts.Homeworks.hw0627.page_objects.add_member_page import AddMemberPage

        self.driver.refresh()
        time.sleep(2)

        self.wait_for_click(self._ADD_MEMBER)

        return AddMemberPage(self.driver)

    def click_add_dept(self):
        """
        点击"添加部门"
        :return:
        """
        from Hogwarts.Homeworks.hw0627.page_objects.add_dept_page import AddDeptPage

        self.find_and_click(*self._ADD_DEPT)
        time.sleep(1)
        self.find_and_click(*self._MENU_ADD_SUB_DEPT)

        return AddDeptPage(self.driver)

    def get_member_names(self):
        """
        获取成员名字，方便后续断言使用
        :return:
        """
        self.driver.refresh()
        time.sleep(2)

        name_list = []
        elements = self.finds(*self._PERSON_NAMES)
        for value in elements:
            name_list.append(value.get_attribute("title"))
        return name_list

    def get_dept_names(self):
        """
        获取部门名字，方便后续断言使用
        :return:
        """
        self.driver.refresh()
        time.sleep(2)

        dept_name_list = []
        elements = self.finds(*self._DEPT_NAMES)
        for ele in elements:
            dept_name_list.append(ele.text)
        return dept_name_list
